﻿using System;
using System.Collections;
using System.Text;
using TADF.Com.Alpha.Core;
using TADF.Com.Alpha.Core.DB;
using TADF.Com.Alpha.Core.View;
using TADF.Com.Alpha.Core.View.Component;

using TADF.Model;

namespace TADF.BLL
{
    class UserAction : TotemRpcServiceBase
    {
        private EntityControl control;

        public UserAction()
		{
			control = EntityControl.CreateEntityControl("TADF.Model");
		}

        [TotemRpcMethod("Login")]
        public string Login(string username, string password)
        {
            string strHQL = "from User";
            UserAction bll = new UserAction();
            IList lst = bll.GetAllUsers(strHQL);
            if (lst.Count > 0)
            {
               return ((User)lst[0]).EmailAddress.ToString();
            }
            else
            {
                return "failed";
            }            
        }

        [TotemRpcMethod("LoginInit")]
        public DataView LoginInit()
        {
            DataView dv = new DataView();
            string strHQL = "from User";
            UserAction bll = new UserAction();
            IList lst = bll.GetAllUsers(strHQL);
            if (lst.Count > 0)
            {
                //return lst;
                //return ((User)lst[0]).EmailAddress.ToString();
                User user = (User)lst[0];

                dv.AddComponent(new Text("username", user.UserName.ToString()));
                dv.AddComponent(new Text("password", user.Password.ToString()));

                return dv;
            }
            else
            {
                return null;
            }
        }


		public void AddUser(User user)
		{
			control.AddEntity(user);
		}
             
		public void UpdateUser(User user,string Id)
		{
			control.UpdateEntity(user,user.Id);
		}
         
		public void DeleteUser(User user)
		{
			control.DeleteEntity(user);
		} 
		
		public IList GetAllUsers(string strHQL)
		{
			return control.GetEntities(strHQL);
		}
    }  
}


